#Import Modules
import os, pygame
from pygame.locals import *

from Character import Character
from ControllerInterface import ControllerInterface

# Load up our helper functions
from Loaders import *

class Jumper(Character):
    """this object contains the player character"""
    def __init__(self, spawnpoint, target):
        Character.__init__(self, spawnpoint, JumperController(self,target)) #call Sprite initializer
        
        # Initial image, collision mask
        self.image, self.rect = load_image(os.path.join('data','characters','jumper',"ball.png"))
        self.original = self.image

        self.padx = -20
        self.pady = -20
        
        # Non-standard Physical Vars
        self.friction = 0.1
        self.elasticity = 0.9
        self.health = 60
        
        # Animation frames
        
        # SFX
        
    def update(self):
        # Run the AI routines
        self.controller.update()
        
        # Let the parent class process the AI input into movement.
        Character.update(self)
        
        # Animate any movement
        
        # Trigger any SFX
        
        # Handle death
        if self.dying == 1:
            self.dead = 1
            
class SpikeJumper(Jumper):
    firstinit = True
    image = None
    rect = None
    sfx = list()
    
    def __init__(self, spawnpoint, target):
        
        if SpikeJumper.firstinit:
            SpikeJumper.firstinit = False
            SpikeJumper.image, SpikeJumper.rect = load_image(os.path.join('data','characters','rocketspike',"rocketspike.png"))
            SpikeJumper.sfx.append(load_sound("metalhit.wav"))
            SpikeJumper.sfx.append(load_sound("hollowmetalhit.wav"))
    
        Jumper.__init__(self, spawnpoint, target)
        self.image, self.rect = SpikeJumper.image, Rect(SpikeJumper.rect)
        self.padx = -30
        self.pady = -30
        # This object has a perminant attack box (it's covered in spikes)
        self.attackboxes.append((25,Rect(5,5,40,40)))
    
    def hit(self, damage, stun=20, direction=-1, knockback=10):
        if Jumper.hit(self,damage,stun,direction,knockback):
            SpikeJumper.sfx[1].play()
    
    def update(self):
        Jumper.update(self)
        if self.landing == 1:
            SpikeJumper.sfx[0].play()
        
class MusicalJumper(Jumper):
    firstinit = True
    frames = list()
    rect = None
    sfx = list()
    lastnote = -1
    
    def __init__(self, spawnpoint, target, note=-1):
        if MusicalJumper.firstinit:
            MusicalJumper.firstinit = False
            # C Major scale
            MusicalJumper.sfx.append(load_sound("C1.wav"))
            #MusicalJumper.sfx.append(load_sound("C#1.wav"))
            MusicalJumper.sfx.append(load_sound("D1.wav"))
            #MusicalJumper.sfx.append(load_sound("D#1.wav"))
            MusicalJumper.sfx.append(load_sound("E1.wav"))
            MusicalJumper.sfx.append(load_sound("F1.wav"))
            #MusicalJumper.sfx.append(load_sound("F#1.wav"))
            MusicalJumper.sfx.append(load_sound("G1.wav"))
            #MusicalJumper.sfx.append(load_sound("G#1.wav"))
            MusicalJumper.sfx.append(load_sound("A1.wav"))
            #MusicalJumper.sfx.append(load_sound("A#1.wav"))
            MusicalJumper.sfx.append(load_sound("B1.wav"))
            MusicalJumper.sfx.append(load_sound("C2.wav"))
        
        Jumper.__init__(self, spawnpoint, target)
        
        if note == -1:
            if MusicalJumper.lastnote < len(MusicalJumper.sfx) - 1:
                MusicalJumper.lastnote += 1
            else:
                MusicalJumper.lastnote = -1
            self.note = MusicalJumper.lastnote
        else:
            self.note = note

    def update(self):
        Jumper.update(self)
        if self.landing == 1:
            MusicalJumper.sfx[self.note].play()

class PlayerJumper(Jumper):
    def __init__(self, spawnpoint, controller):
        Jumper.__init__(self, spawnpoint, None)
        self.controller = controller
            
class JumperController(ControllerInterface):
    def __init__(self,me,target):
        ControllerInterface.__init__(self)
        self.me = me
        self.target = target
    def update(self):
        ControllerInterface.update(self)
        # Run directly at the target
        if self.me.positionx > self.target.positionx:
            self.set_left(1)
            self.set_right(0)
        if self.me.positionx < self.target.positionx:
            self.set_left(0)
            self.set_right(1)
        # Jump as high as possible all the time
        if self.me.jumpframes == self.me.maxjumpframes:
            self.set_jump(0)
        elif self.me.jump == 0 and self.me.walk == 1 and self.me.jumpframes == 0:
            self.set_jump(1)
            
    